<?php
load_info_var($cf_controller);											//加载smarty变量
load_file('news_class',LIB_PATH);
$main = new News($db);
$id = isset($_POST['id']) ? intval($_POST['id']) : intval($_GET['id']);
$return_page = $cf_pre_page ? $cf_pre_page : $cf_self_page;
//print_r($cf_admin_info);
switch($cf_action){
	case "index":
		$smarty->mypage($cf_controller, $cf_action, 0);       
        $stype = isset($_POST['stype']) ? $_POST['stype'] : $_GET['stype'];        
        $skey = isset($_POST['skey']) ? trim($_POST['skey']) : urldecode(trim($_GET['skey']));       
        $where = "1";

        if ($skey) {
            $where .= " AND news_title like '%" . $skey . "%'";
        }
		if ($stype) {
            $where .= " AND news_sort ='" . $stype . "'";
        }
        load_file('page_class', LIB_PATH);
        $MyPage = new Mypage($db);
		$field = '*';		
        $data_ary = $MyPage->from_sql($field, $main->get_table_name(), $where, "news_id DESC", intval($_GET['cpage']), "20");        
		$page = $MyPage->get_page($data, $cf_self_page . '?dpt='.$cf_dpt.'&skey=' . $skey . '&stype=' . $stype);
		
	break;
	case "add":
		if($cf_ispost){
			$data = $db->getFormData($main->get_table_name(),$_POST);
			$data['news_create_time'] = date("Y-m-d H:i:s");	
			$result = $main->insert_data($data);
			if($result){
				$return_page = $_SESSION['pre_url'] ? $_SESSION['pre_url'] : $cf_self_page;
				unset($_SESSION['pre_url']);
				$more = $_SERVER['HTTP_REFERER'] ? '<a onclick="my_close()" href="' . $_SERVER['HTTP_REFERER'] . '">[继续添加]</a>' : '';
				my_jump('添加成功'.$more,'',$return_page.'?dpt='.$cf_dpt,3);
			}else{
			my_jump('添加失败');
			}
		}else{			
			$news_sort = $cf_info_var_common['news_sort'];
			unset($news_sort[0]);			
			$smarty->mypage($cf_controller,$cf_action,0);	
			$_SESSION['pre_url'] = $cf_pre_page ? $cf_pre_page : '';	
		}

    break;
	
	case "edit":
		if($cf_ispost){
			$data = $db->getFormData($main->get_table_name(),$_POST);
			$data['news_edit_info']=$cf_admin_info['admin_name'].' '.date("Y-m-d H:i");

			$result = $main->update_data($data,"news_id='".$id."'");
			if($result){
				$return_page = $_SESSION['pre_url'] ? $_SESSION['pre_url'] : $cf_self_page;
				unset($_SESSION['pre_url']);
				my_jump('修改成功','',$return_page.'?dpt='.$cf_dpt,1);
            }else{
				my_jump('修改失败');
            }
		}else{
			$smarty->mypage($cf_controller,$cf_action,0) ;
			$news_sort = $cf_info_var_common['news_sort'];
			unset($news_sort[0]);
			$row = $main->get_data_row($id);
			if(!$row){
				my_jump('此信息不存在或已被删除');
			}		
			$_SESSION['pre_url'] = $cf_pre_page ? $cf_pre_page : '';	
		}
    break;
	
	case "del":			
		$result = $main->del_data("news_id='$id'");		
		if($result){
			$return_page = $_SESSION['pre_url'] ? $_SESSION['pre_url'] : $cf_self_page;
			unset($_SESSION['pre_url']);			
			my_jump('删除成功','',$return_page.'?dpt='.$cf_dpt,1);
		}else{
			my_jump('删除失败');
		}
	break;
	case "delphoto":
		$row = $news->get_data_row($id);
		if(!$row){
			my_jump('This information does not exist or has been deleted');
		}
		$news->del_photo($row['news_photo']);
		$result = $news->update_data(array('news_photo'=>''),"news_id='".$id."'");
		if($result)
			my_jump('更新完成','',$cf_self_page,1);
		else
			my_jump('更新失败');
	break;
	case "set":
		$type = $_GET['type'];
		$value = $_GET['value'];
		$where = "news_id = '".$id."'";
		$result = $news->set_data($value,$type,$where);
		if($result)
			my_jump('更新完成','',$return_page.'?dpt='.$cf_dpt,1);
		else 
			my_jump('更新失败');
	break;
	case "all":
		$alltype = $_POST['alltype'];
		$idlist = $_POST['idlist'];
		if($idlist){
			$list = implode(",",$idlist);
			switch($alltype){
				case "alldel":
					$result = $news->del_data("news_id in (".$list.")");
				break;
				case "allhot":
					$data = array('news_hot'=>'1');
					$result = $news->update_data($data,"news_id in (".$list.")");
				break;
				case "allunhot":
					$data = array('news_hot'=>'0');
					$result = $news->update_data($data,"news_id in (".$list.")");
				break;
				case "allshow":
					$data = array('news_show'=>'1');
					$result = $news->update_data($data,"news_id in (".$list.")");
				break;
				case "allunshow":
					$data = array('news_show'=>'0');
					$result = $news->update_data($data,"news_id in (".$list.")");
				break;
				case "allcheck":
					$data = array('news_check'=>'1');
					$result = $news->update_data($data,"news_id in (".$list.")");
				break;
				case "alluncheck":
					$data = array('news_check'=>'0');
					$result = $news->update_data($data,"news_id in (".$list.")");
				break;
				case "allcomment":
					$data = array('news_comment'=>'1');
					$result = $news->update_data($data,"news_id in (".$list.")");
				break;
				case "alluncomment":
					$data = array('news_comment'=>'0');
					$result = $news->update_data($data,"news_id in (".$list.")");
				break;
				default:
					my_jump('请指定操作方式');
				break;
			}	
			if($result)
				my_jump('更新完成','',$cf_self_page,1);
			else
				my_jump('更新失败');
			
		}else{
			my_jump('输入的参数有误');	
		}
	break;
	default:
		my_jump('非法操作!');
	break;
}

$smarty->assign(array(
	'id'=>$id,
	'rid'=>$rid,
	'row'=>$row,
	'data_ary'=>$data_ary,	
	'page'=>$page,
	'skey' => $skey,
	'stype' => $stype,
	'news_sort' => $news_sort,
	'cf_action' => $cf_action,

));
$smarty->mydisplay();

?>